package com.xiangyang.pos.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;

import com.xiangyang.pos.R;

/**
 * Created by Administrator on 2017/9/1 0001.
 *
 * 网页加载
 *
 * @author YeDalin
 *
 */

public class WebActivity extends Activity {

    private ImageView ivGoBack;
    private WebView webView;

    private String url;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
        initView();
        getLastIntent();
        setMyClick();
        setWebView();

    }

    private void initView(){
        ivGoBack=findViewById(R.id.iv_go_back);
        webView=new WebView(this);
        webView=findViewById(R.id.web_view);


        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setPluginState(WebSettings.PluginState.ON);
        webView.getSettings().setUseWideViewPort(true);

    }

    private void getLastIntent(){
        Intent intent = getIntent();
        url = intent.getStringExtra("url");
    }

    private void setMyClick(){
        ivGoBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(WebActivity.this,FirstActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    private void setWebView(){
        webView.loadUrl(url);
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return true;
            }
        });
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
            webView.goBack();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(webView!=null){
            webView.destroy();
        }
    }
}
